// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_android: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var res = wx.getSystemInfoSync()
    console.log(res.platform)
    if (wx.getStorageSync('os') == 'ios') {
      this.setData({
        is_android: false,
      })
    }
    this.getPowerOrder()
  },
  addSend(){
    wx.requestSubscribeMessage({
      tmplIds: ['ejT7D2ryZ2w-DEYOdZSa2fnMN-KqGjpIsrPzewMfWTk'],
      success (res) {
        console.log(res)
       if(res.errMsg== "requestSubscribeMessage:ok"){
        wx.showLoading({
          title:'加载中'
        })
        wx.cloud.database().collection('p_pushlist').add({
          data:{
          os: wx.getStorageSync('os'),
          token: wx.getStorageSync('token'),
          userId: wx.getStorageSync('user').id
          }
        }).then(res=>{
          console.log(res)
          wx.hideLoading()
          wx.showToast({
            title: '订阅成功',
          })
        })
       }
      
        }
      })
  },
  onShow() {
    var res = wx.getSystemInfoSync()
    console.log(res.platform)
    if (wx.getStorageSync('os') == 'ios') {
      this.setData({
        is_android: false,
      })
    }
    this.getPowerOrder()
  },
  formatTimeTwo(number, format) {
    var dateTime = new Date(number)
    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var hour = dateTime.getHours();
    var minute = dateTime.getMinutes();
    var second = dateTime.getSeconds();
    var timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
    return timeSpanStr;
  },
  getPowerOrder() {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: "getOrderinfo",
      data: {
        os: this.data.is_android ? 'android' : 'ios',
        userId: wx.getStorageSync('user').id,
        token: wx.getStorageSync('token'),
      }
    }).then(res => {

     
      let bkdata = JSON.parse(res.result)
      console.log("bk成功", bkdata)
      var i
      for (i in bkdata.data) {
        bkdata.data[i].start_date = this.formatTimeTwo(bkdata.data[i].beginTime)
        bkdata.data[i].end_date = this.formatTimeTwo(bkdata.data[i].endTime)
      }
      this.setData({
        powerList: bkdata.data
      })
      console.log(bkdata)
      wx.hideLoading()
    }).catch(res => {
      console.log("失败", res)
      wx.showToast({
        title: '系统错误！',
        icon: 'none'
      })
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   *  if(bkdata.data[0].status == 0){
       var alltime = bkdata.data[0].minutes
       var begintime = bkdata.data[0].beginTime
       var nowtime =  Date.parse(new Date())
       var pop = (nowtime - begintime)/60/1000/alltime
      this.setData({
        pop : parseInt(pop),
        alltime,
        status: bkdata.data[0].status
      })
     }else{

      let a =  this.formatTimeTwo(bkdata.data[0].beginTime)
      console.log(a)
       this.setData({
        status: bkdata.data[0].status,
        order: bkdata.data[0],
        a
       })
     }
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})